package cz.kfad.ofrss.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import cz.kfad.ofrss.R;
import cz.kfad.ofrss.activity.AllFeedsActivity;
import cz.kfad.ofrss.activity.RssFeedActivity;
import cz.kfad.ofrss.backend.BackendService;
import cz.kfad.ofrss.backend.IBackend;
import cz.kfad.ofrss.db.DbConstants;
import cz.kfad.ofrss.model.RssFeedShort;
import cz.kfad.ofrss.util.CollectionsUtil;

import java.util.List;

public class Widget extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);

        ComponentName thisWidget = new ComponentName(context, Widget.class);

        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

        for (int widgetId : allWidgetIds) {
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
            IBackend backend = BackendService.createBackend(context);
            List<RssFeedShort> feeds = backend.findAllFeeds(DbConstants.COLUMN_RSS_FEED_PUBDATE + DbConstants.DESC);
            if (feeds.isEmpty()) {
                remoteViews.setTextViewText(R.id.widget_text, context.getString(R.string.widget_noRssFound));
                remoteViews.setTextViewText(R.id.widget_number, "0");
                remoteViews.setTextViewText(R.id.widget_date, "");
            } else {
                RssFeedShort rssFeedShort = feeds.get(0);
                Intent intent = new Intent(context, AllFeedsActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
                remoteViews.setTextViewText(R.id.widget_text, rssFeedShort.getTitle());
                remoteViews.setTextViewText(R.id.widget_date, rssFeedShort.getDate().toLocaleString());

                remoteViews.setTextViewText(R.id.widget_number, String.valueOf(backend.getCountUnreadFeeds()));
                remoteViews.setOnClickPendingIntent(R.id.widget_text, pendingIntent);
            }

            appWidgetManager.updateAppWidget(widgetId, remoteViews);
        }
    }

    // TODO asi delete zobrazit vse mi prijde lepsi
    private PendingIntent createPendingIntent(Context context, List<RssFeedShort> feeds, RssFeedShort rssFeedShort) {
        final Intent intent = new Intent(context, RssFeedActivity.class);
        intent.putExtra(RssFeedActivity.RSS_ID_KEY, rssFeedShort.getId());
        intent.putExtra(RssFeedActivity.RSS_FEED_IDS_KEY, CollectionsUtil.createFeedIdsArray(feeds));
        intent.putExtra(RssFeedActivity.RSS_TITLE_KEY, R.string.allFeedsByDateTitle);
        System.out.println("Extra ID: " + intent.getExtras().get(RssFeedActivity.RSS_ID_KEY));
        return PendingIntent.getActivity(context, 0, intent, 0);
    }

}
